home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_13_01 / thomas / next.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-31  |  790 b   |  25 lines

  1. Listing 3: Finding the next value
  2.  
  3. float x1, x2, x3, x4, x5;<R>
  4. x1 = 0x0.800001p1;          /* hex floating constant */<R>
  5. x2 = nextafterf(1.0, 2.0);  /* next from 1 to 2 */<R>
  6. x3 = 1.00000012f;           /* 2^-23 approx= 1.2e-7 */<R>
  7. x4 = 1.0 + pow(2.0, -23.0); /* evaluate increment */<R>
  8. x5 = nextrep(1.0f);         /* defined later */<R>
  9.  
  10.  
  11. Listing 4: The function nextafter
  12.  
  13. #pragma fenv_access on<R>
  14. #pragma fp_wide_function_parameters off /* default */<R>
  15. float nextrep(float x) { /* 32-bit IEEE value */<R>
  16.     fenv_t saved_env;<R>
  17. <R>
  18.     (void) feholdexcept(&saved_env);<R>
  19.     fesetround(FE_UPWARD); /* + will round up */<R>
  20.     x += 0x1.0p-149; /* add tiniest subnormal */<R>
  21.     fesetenv(&saved_env); /* restore rounding, flags */<R>
  22.     return x;<R>
  23. }
  24.  
  25.